<?php

namespace app\common\model\mycurrency;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class MerchantMoneyWithdrawal extends Model
{

    //use SoftDelete;

    

    // 表名
    protected $name = 'mycurrency_merchant_money_withdrawal';

    const CHANGE_TYPE_MERCHANT = 1;//变动身份 商铺余额变动
    const CHANGE_TYPE_STORE = 2;//变动身份 门店余额变动

    const PAYMENT_MODE_WEIXIN               = 1;//提现方式:1=微信打款
    const PAYMENT_MODE_ZHIFUBAO             = 2;//提现方式:2=支付宝打款
    const PAYMENT_MODE_YINGHANGKA           = 3;//提现方式:3=银行卡线下打款
    const PAYMENT_MODE_WEIXINSHOUKUANMA     = 4;//提现方式:4=微信收款码收款
    const PAYMENT_MODE_ZHIFUBAOSHOUKUANMA   = 5;//提现方式:5=支付宝收款码收款

    const STATUS_QUXIAO                     = -1;//状态 - 取消
    const STATUS_BOHUI                      = -2;//状态 - 提现驳回
    const STATUS_DAICHULI                   = 1;//状态 - 待处理
    const STATUS_DAIDAKUAN                  = 2;//状态 - 审核通过待打款
    const STATUS_DAKUANZHONG                = 3;//状态 - 打款中
    const STATUS_YIDAKUAN                   = 4;//状态 - 已打款

    /**
     * 商铺\门店余额提现
     * @param int $change_type 变动身份:1=商铺余额变动,2=门店余额变动
     * @param int $merchant_id 商铺ID
     * @param int $store_id 门店ID
     * @param int $money 提现金额
     * @param int $payment_mode 提现打款方式 1=微信打款,2=支付宝打款,3=银行卡线下打款,4=微信收款码收款,5=支付宝收款码收款
     * @param array $receive 收款信息 银行卡打款【卡号，开户行，开户人】 微信收款码打款【微信收款码图片地址】 支付宝收款码打款【支付宝收款码地址】
     * @param string $explain 体现说明
     */
    public static function withdrawal($change_type,$merchant_id,$store_id,$money,$payment_mode,$receive = [],$explain = null){
        $config = MerchantConfig::where(['id' => 1])->find();
        if($change_type == self::CHANGE_TYPE_MERCHANT){//商铺体现
            $merchant = Merchant::get($merchant_id);
        }elseif($change_type == self::CHANGE_TYPE_STORE){//门店体现
            $merchant = MerchantStore::get($store_id);
        }

        if($merchant->money < $money){
            throw new Exception('余额不足');
            return false;
        }
        $service_charge = number_format(( $money / 100 ) * $config['witserpro'],2,'.','') ;//手续费
        $actual_payment = $money - $service_charge;//实际需要打款金额
        $increase = [
            'change_type' => $change_type,
            'money' => $money,
            'witserpro' => $config['witserpro'],
            'service_charge' => $service_charge,
            'actual_payment' => $actual_payment,
            'payment_mode' => $payment_mode,
            'explain' => $explain,
            'status' => self::STATUS_DAICHULI,
            'applytime' => time(),
            'createtime' => time(),
        ];
        if($change_type == self::CHANGE_TYPE_MERCHANT){//商铺余额变动
            $increase['merchant_id'] = $merchant_id;
        }elseif($change_type == self::CHANGE_TYPE_STORE){//门店余额变动
            $increase['store_id'] = $store_id;
        }

        if($payment_mode == self::PAYMENT_MODE_YINGHANGKA){//提现方式:3=银行卡线下打款
            $increase['bank_number'] = $receive['bank_number'];
            $increase['bank_opening'] = $receive['bank_opening'];
            $increase['bank_account'] = $receive['bank_account'];
        }elseif ($payment_mode == self::PAYMENT_MODE_WEIXINSHOUKUANMA){//提现方式:4=微信收款码收款
            $increase['wechat_code_image'] = $receive['wechat_code_image'];
        }elseif ($payment_mode == self::PAYMENT_MODE_ZHIFUBAOSHOUKUANMA){//提现方式:5=支付宝收款码收款
            $increase['alipay_code_image'] = $receive['alipay_code_image'];
        }

        $withdrawal = self::create($increase);

        $memo = $change_type == self::CHANGE_TYPE_MERCHANT ? "商铺" : "门店";
        $memo .= '收益提现';
        MerchantMoneyLog::moneyChange($change_type,$merchant_id,$store_id,$money,MerchantMoneyLog::TYPE_TIXIAN,[
            'memo' => $memo,
            'withdrawal_id' => $withdrawal->id,
        ]);
    }


}
